package com.share.thread;


/**
 * @desc: 两个线程打印 12A 34B  5152Z
 * @author:caifan
 * @date:2022/1/19
 */
public class TakeTurn01 {
    public static void main(String[] args) {
        Print01 print01 = new Print01();

        Thread t1 = new Thread(() -> print01.print1());
        Thread t2 = new Thread(() -> print01.print2());

        t1.start();
        t2.start();
    }
}


class Print01 {
    private char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private volatile boolean flag = true;

    private static int count = 0;

    public synchronized void print1() {
        for (int i = 1; i <= 26; i++) {
            while (!flag) {
                try {
                    wait();
                } catch (Exception e) {

                }
            }

            flag = !flag;
            notifyAll();
            System.out.println("print1:" + ++count);
            System.out.println("print1:" + ++count);
        }
    }

    public synchronized void print2() {
        for (int i = 1; i <= 26; i++) {
            while (flag) {
                try {
                    wait();
                } catch (Exception e) {

                }
            }

            flag = !flag;
            notifyAll();
            System.out.println("print2:" + chars[i - 1]);
        }
    }

}
